package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements RoomInfoService {
   @Autowired
   private GraphInfoService graphInfoService;

   @Autowired
   private RoomAttrValueService roomAttrValueService;

   @Autowired
   private RoomFacilityService roomFacilityService;

   @Autowired
   private RoomLabelService roomLabelService;

   @Autowired
   private RoomPaymentTypeService roomPaymentTypeService;

   @Autowired
   private RoomLeaseTermService roomLeaseTermService;

   @Autowired
   private RoomInfoMapper roomInfoMapper;

   @Autowired
   private ApartmentInfoMapper apartmentInfoMapper;

   @Autowired
   private GraphInfoMapper graphInfoMapper;

   @Autowired
   private AttrValueMapper attrValueMapper;


   @Autowired
   private FacilityInfoMapper facilityInfoMapper;

   @Autowired
   private LabelInfoMapper labelInfoMapper;

   @Autowired
   private PaymentTypeMapper paymentTypeMapper;

   @Autowired
   private LeaseTermMapper leaseTermMapper;


    @Override
    public RoomDetailVo listroomById(Long id) {
        //查询房间信息
        RoomInfo room = getById(id);

        //表查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentByroomId(id);

        //查询图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphByRoomId(id,ItemType.ROOM);

        //通过中间表查询属性信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrByroomId(id);

        //通过中间表查询配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityByroomId(id);

        //通过中间表查询标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelByroomId(id);

        //通过中间表查询支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentByroomId(id);

        //通过中间表查询租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseByroomId(id);

        //把查询出来的数据放到一个几个
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        BeanUtils.copyProperties(room,roomDetailVo);
        return roomDetailVo;
    }

    @Override
    public void pageRoomList(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
           roomInfoMapper.pageRoomSelect(page,queryVo);
    }

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {

        //在对roominfo表进行操作时，先判断id是否为空，防止mybatis—plus的id回显
        //不为空则，则为更新。更新需要先删除，后添加
        if(roomSubmitVo.getId()!= null){
            //删除图片表
            LambdaUpdateWrapper<GraphInfo> graphInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            graphInfoLambdaUpdateWrapper.eq(GraphInfo::getItemId,roomSubmitVo.getId());
            graphInfoLambdaUpdateWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(graphInfoLambdaUpdateWrapper);

            //删除属性信息中间表
            LambdaUpdateWrapper<RoomAttrValue> roomAttrValueLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            roomAttrValueLambdaUpdateWrapper.eq(RoomAttrValue::getRoomId,roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueLambdaUpdateWrapper);

            //删除配套信息中间表
            LambdaUpdateWrapper<RoomFacility> roomFacilityLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            roomFacilityLambdaUpdateWrapper.eq(RoomFacility::getRoomId,roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityLambdaUpdateWrapper);

            //删除标签信息中间表
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomSubmitVo.getId());
            roomLabelService.remove(roomLabelLambdaQueryWrapper);

            //删除支付方式中间表
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

            //删除租期中间表
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }

        //添加信息
            //添加房间信息
        /*RoomInfo roomInfo = new RoomInfo();
        roomInfo.setId(roomSubmitVo.getId());
        roomInfo.setRoomNumber(roomSubmitVo.getRoomNumber());
        roomInfo.setRent(roomSubmitVo.getRent());
        roomInfo.setApartmentId(roomSubmitVo.getApartmentId());
        roomInfo.setIsRelease(roomSubmitVo.getIsRelease());*/
        saveOrUpdate(roomSubmitVo);

            //添加图片表
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfos = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }

            //添加属性信息中间表
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if(!CollectionUtils.isEmpty(attrValueIds)){
            ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>(attrValueIds.size());
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue build = RoomAttrValue.
                        builder().
                        attrValueId(attrValueId).
                        roomId(roomSubmitVo.getId()).
                        build();
                roomAttrValues.add(build);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }

            //添加配套信息中间表
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility build = RoomFacility.
                        builder().
                        roomId(roomSubmitVo.getId()).
                        facilityId(facilityInfoId).
                        build();
                roomFacilities.add(build);
            }
            roomFacilityService.saveBatch(roomFacilities);

        }

            //添加标签信息中间表
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if(!CollectionUtils.isEmpty(labelInfoIds)){
            ArrayList<RoomLabel> roomLabels = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel build = RoomLabel.builder().
                        roomId(roomSubmitVo.getId()).
                        labelId(labelInfoId).
                        build();
                roomLabels.add(build);
            }
            roomLabelService.saveBatch(roomLabels);
        }

            //添加支付方式中间表
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if(!CollectionUtils.isEmpty(paymentTypeIds)){
            ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType build = RoomPaymentType.builder().
                        roomId(roomSubmitVo.getId()).
                        paymentTypeId(paymentTypeId).
                        build();
                roomPaymentTypes.add(build);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }

        //添加租期中间表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if(!CollectionUtils.isEmpty(leaseTermIds)){
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm build = RoomLeaseTerm.builder().
                        roomId(roomSubmitVo.getId()).
                        leaseTermId(leaseTermId).
                        build();
                roomLeaseTerms.add(build);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }
}




